package com.google.code.tretris;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SettingsDialog extends Dialog {

	public static final String PREFS_NAME = "settings";
	public static final String USER_ID = "username";
	public static final String PASSWORD = "password";
	
	private final SharedPreferences settings;
	
	public SettingsDialog(Context context) {
		super(context);
		settings = context.getSharedPreferences(PREFS_NAME, 0);
	}
	
    protected void onStart() {
        super.onStart();
        setContentView(R.layout.settings);
        getWindow().setFlags(4, 4);
        setTitle("Settings");
        
        setText(R.id.userid, settings.getString(USER_ID, ""));
        setText(R.id.password, settings.getString(PASSWORD, ""));
        
        Button save = (Button) findViewById(R.id.settings_save);
        save.setOnClickListener(new android.view.View.OnClickListener() {
			
			public void onClick(View v) {
		        String username = getText(R.id.userid);
		        String password = getText(R.id.password);
		        SharedPreferences.Editor editor = settings.edit();
		        editor.putString(USER_ID, username);
		        editor.putString(PASSWORD, password);
		        editor.commit();
		        dismiss();
			}
		});
    }
    
    
    private String getText(int id) {
        EditText widget = (EditText) this.findViewById(id);
        return widget.getText().toString();
    }
    
    private void setText(int id, String value) {
        EditText widget = (EditText) this.findViewById(id);
        widget.setText(value);
    }
}
